﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Data.SqlClient;

namespace GUI
{
    public partial class usThietBiDien : UserControl
    {
        public usThietBiDien()
        {
            InitializeComponent();
        }

        private void usThietBiDien_Load(object sender, EventArgs e)
        {
            LoadLoaiThietBiDien();
            LoadThietBiDien();
            grbThietBiDien.Visible = false;
        }

        private void TrangThai(bool status)
        {
            //tsThem.Enabled = status;

            btnThem.Visible = !status;
            btnSua.Visible = status;
        }

        private void TrangThai_LoaiTB(bool status)
        {
            //tsThem.Enabled = status;

            btnThemLoai.Visible = !status;
            btnSuaLoai.Visible = status;
        }

        private void reset()
        {
            dtpNgayNhap.Text = (DateTime.Now).ToString();
            txtMaTB.Text = "";
            txtTenTB.Text = "";
            cbbLoaiTB.SelectedValue = 1;
            //txtV.Text = "";
            //txtA.Text = "";
            txtCS.Text = "";
            txtCSHH.Text = "0";
            txtHSDB.Text = "1";
            txtGia.Text = "0";

            LoadThietBiDien();
        }

        private void LoadLoaiThietBiDien()
        {
            ThietBiDienBUS tbdBUS = new ThietBiDienBUS();
            cbbLoaiTB.DataSource = tbdBUS.LOADLOAITHIETBIDIEN();
            cbbLoaiTB.DisplayMember = "TenLoai";
            cbbLoaiTB.ValueMember = "MaLoai";
        }

        private void LoadThietBiDien()
        {
            ThietBiDienBUS tbdBUS = new ThietBiDienBUS();
            gvTBDien.DataSource = tbdBUS.LOADTHIETBIDIEN();

            SetupDataGridView(gvTBDien);
        }

        private void SetupDataGridView(DataGridView gv)
        {
            DataGridViewCellStyle style = gv.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(gv.Font, FontStyle.Bold);


            gv.GridColor = Color.Blue;  //' Màu các của lưới
            gv.BackgroundColor = Color.AliceBlue; //' Màu nền gridview
            gv.ForeColor = Color.Green; //' Màu chữ
            //' Thiết lập màu chữ cho những dòng có chỉ số lẻ
            gv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple;
        }

        private void ThongTinTB(ThietBiDienDTO tbdDTO)
        {
            tbdDTO.MATB = txtMaTB.Text;
            tbdDTO.TENTB = txtTenTB.Text.Trim();
            tbdDTO.NGAYNHAP = Convert.ToDateTime(dtpNgayNhap.Text);
            tbdDTO.CSKHADUNG = int.Parse(txtCS.Text);
            if (rbiCSHH.Checked)
                tbdDTO.CSHAOHUT = int.Parse(txtCSHH.Text);
            else
                tbdDTO.CSHAOHUT = tbdDTO.CSKHADUNG * int.Parse(txtCSHH.Text) / 100;
            if(!string.IsNullOrEmpty(txtHSDB.Text))
                tbdDTO.HSDEBA = int.Parse(txtHSDB.Text);
            if (!string.IsNullOrEmpty(txtGia.Text))
                tbdDTO.DONGIA = int.Parse(txtGia.Text);
            tbdDTO.MALOAI = int.Parse(cbbLoaiTB.SelectedValue.ToString());
        }

        //them thiet bi dien
        private void THEMTHIETBIDIEN()
        {
            ThietBiDienDTO tbdDTO = new ThietBiDienDTO();

            tbdDTO.STT = 0;
            //tbdDTO.VOL = Convert.ToInt32(txtV.Text);
            //tbdDTO.AMP = Convert.ToInt32(txtA.Text);
            tbdDTO.GHICHU = "";

            tbdDTO.MATB = txtMaTB.Text;
            tbdDTO.TENTB = txtTenTB.Text.Trim();
            tbdDTO.NGAYNHAP = Convert.ToDateTime(dtpNgayNhap.Text);
            tbdDTO.CSKHADUNG = int.Parse(txtCS.Text);
            if (rbiCSHH.Checked)
                tbdDTO.CSHAOHUT = int.Parse(txtCSHH.Text);
            else
                tbdDTO.CSHAOHUT = tbdDTO.CSKHADUNG * int.Parse(txtCSHH.Text) / 100;
            tbdDTO.HSDEBA = 1;
            tbdDTO.DONGIA = 0;
            if (!string.IsNullOrEmpty(txtHSDB.Text))
                tbdDTO.HSDEBA = int.Parse(txtHSDB.Text);
            if (!string.IsNullOrEmpty(txtGia.Text))
                tbdDTO.DONGIA = int.Parse(txtGia.Text);
            tbdDTO.MALOAI = int.Parse(cbbLoaiTB.SelectedValue.ToString());

            //ThongTinTB(tbdDTO);

            ThietBiDienBUS tbdBUS = new ThietBiDienBUS();
            tbdBUS.THEMTHIETBIDIEN(tbdDTO);
            reset();
        }

        private void SUATHIETBIDIEN()
        {
            ThietBiDienDTO tbdDTO = new ThietBiDienDTO();

            tbdDTO.STT = tbdDTO1.STT;
            //tbdDTO.VOL = Convert.ToInt32(txtV.Text);
            //tbdDTO.AMP = Convert.ToInt32(txtA.Text);
            tbdDTO.GHICHU = "";

            tbdDTO.MATB = txtMaTB.Text;
            tbdDTO.TENTB = txtTenTB.Text.Trim();
            tbdDTO.NGAYNHAP = Convert.ToDateTime(dtpNgayNhap.Text);
            tbdDTO.CSKHADUNG = int.Parse(txtCS.Text);
            if (rbiCSHH.Checked)
                tbdDTO.CSHAOHUT = int.Parse(txtCSHH.Text);
            else
                tbdDTO.CSHAOHUT = tbdDTO.CSKHADUNG * int.Parse(txtCSHH.Text) / 100;
            tbdDTO.HSDEBA = 1;
            tbdDTO.DONGIA = 0;
            if (!string.IsNullOrEmpty(txtHSDB.Text))
                tbdDTO.HSDEBA = int.Parse(txtHSDB.Text);
            if (!string.IsNullOrEmpty(txtGia.Text))
                tbdDTO.DONGIA = int.Parse(txtGia.Text);
            tbdDTO.MALOAI = int.Parse(cbbLoaiTB.SelectedValue.ToString());

            //ThongTinTB(tbdDTO);

            ThietBiDienBUS tbdBUS = new ThietBiDienBUS();
            tbdBUS.SUATHIETBIDIEN(tbdDTO);
            int alert = tbdBUS.SUATHIETBIDIEN(tbdDTO);
            reset();

            if (alert == 1)
                MessageBox.Show("Đã sửa thiết bị điện " + tbdDTO.STT.ToString() + " thành công");
        }

        private void tsThem_Click(object sender, EventArgs e)
        {
            reset();

            tsThem.Enabled = false;
            tsLoaiTBDien.Enabled = true;

            TrangThai(false);

            if (pnLoai.Visible == true)
                pnLoai.Visible = false;
            if (grbThietBiDien.Visible == false)
                grbThietBiDien.Visible = true;
            if (grbSanPham.Visible == false)
                grbSanPham.Visible = true;
        }

        private void tsLoaiTBDien_Click(object sender, EventArgs e)
        {
            tsThem.Enabled = true;
            tsLoaiTBDien.Enabled = false;

            grbThietBiDien.Visible = false;
            grbSanPham.Visible = false;
            pnLoai.Visible = true;
            pnLoai.Dock = DockStyle.Fill;

            TrangThai_LoaiTB(false);
            LoadLoaiTBDien_GV();
        }

        private void tsDongTTTB_Click(object sender, EventArgs e)
        {
            reset();
            tsThem.Enabled = true;
            tsLoaiTBDien.Enabled = true;

            grbSanPham.Visible = true;
            grbThietBiDien.Visible = false;
            pnLoai.Visible = false;

            btnThemLoai.Visible = true;
            btnSuaLoai.Visible = false;
        }

        private void LoadLoaiTBDien_GV()
        {
            LoaiTBBUS ltbBUS = new LoaiTBBUS();
            gvLoaiTB.DataSource = ltbBUS.LoadLoaiTBDien();

            SetupDataGridView(gvLoaiTB);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            SUATHIETBIDIEN();
            TrangThai(false);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (KiemTraNhap() == 1)
                THEMTHIETBIDIEN();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            reset();

            grbThietBiDien.Visible = false;
            tsThem.Enabled = true;
        }

        ThietBiDienDTO tbdDTO1 = new ThietBiDienDTO();
        private void gvTBDien_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                grbThietBiDien.Visible = true;
                TrangThai(true);

                DataGridViewRow row = gvTBDien.Rows[rowIndex];
                tbdDTO1.STT = int.Parse(row.Cells["STT"].Value.ToString());
                txtMaTB.Text = row.Cells["colMaTB"].Value.ToString();
                txtTenTB.Text = row.Cells["colTenTB"].Value.ToString();
                cbbLoaiTB.Text = row.Cells["colTenLoai"].Value.ToString();
                dtpNgayNhap.Text = row.Cells["colNgayNhap"].Value.ToString();
                //txtV.Text = row.Cells["colVol"].Value.ToString();
                //txtA.Text = row.Cells["colAmp"].Value.ToString();
                txtCS.Text = row.Cells["colCSKhaDung"].Value.ToString();
                txtCSHH.Text = row.Cells["colCSHaoHut"].Value.ToString();
                txtHSDB.Text = row.Cells["colHSDeBa"].Value.ToString();
                txtGia.Text = row.Cells["colDonGia"].Value.ToString();
            }
        }

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)46 && e.KeyChar != (char)8)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                {
                    e.Handled = true;

                }
            }
        }

        private void gvTBDien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa!", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ThietBiDienBUS tbdBUS = new ThietBiDienBUS();
                    int stt = int.Parse(gvTBDien.CurrentRow.Cells["STT"].Value.ToString());

                    tbdBUS.XOATHIETBIDIEN(stt);
                }
                LoadThietBiDien();
            }
        }

        private int KiemTraNhap()
        {
            if (string.IsNullOrEmpty(txtTenTB.Text) || string.IsNullOrEmpty(txtCS.Text))
            {
                MessageBox.Show("Nhập đầy đủ thông tin (*) cần thiết", "Thông Báo!!");
                return 0;
            }
            return 1;
        }

        private void txt_Leave(object sender, EventArgs e)
        {
            if (txtCS.Text != "" && txtCSHH.Text != "")
            {
                int cs = int.Parse(txtCS.Text);
                int cshh = int.Parse(txtCSHH.Text);
                if (cs < cshh)
                {
                    rbptCSHH.Checked = true;
                }
                else
                {
                    rbiCSHH.Checked = true;
                }
            }
            else// if (txtCS.Text == "" || txtCSHH.Text == "")
            {
                rbiCSHH.Checked = true;
            }
        }

        private void tsDong_Click(object sender, EventArgs e)
        {

        }

        private void btnSuaLoai_Click(object sender, EventArgs e)
        {
            LoaiTBBUS ltbBUS = new LoaiTBBUS();
            LoaiTBDTO ltbDTO = new LoaiTBDTO();

            if (txtLoaiTB.Text != "")
            {
                ltbDTO.TENLOAI = txtLoaiTB.Text.Trim();
                int kt = ltbBUS.KTLoaiTBDien(ltbDTO);
                if (kt == 0)
                {
                    ltbDTO.MALOAI = ltbDTO1.MALOAI;
                    ltbBUS.SuaLoaiTBDien(ltbDTO);

                    txtLoaiTB.Text = "";
                    btnThemLoai.Visible = true;
                    btnSuaLoai.Visible = false;
                }
                else
                {
                    MessageBox.Show("Loại " + txtLoaiTB.Text + " đã có", "Thông báo");
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập loại!", "Thông báo");
            }

            TrangThai_LoaiTB(false);
            LoadLoaiTBDien_GV();
        }

        private void btnThemLoai_Click(object sender, EventArgs e)
        {
            LoaiTBBUS ltbBUS = new LoaiTBBUS();
            LoaiTBDTO ltbDTO = new LoaiTBDTO();

            if (txtLoaiTB.Text != "")
            {
                ltbDTO.TENLOAI = txtLoaiTB.Text.Trim();
                int kt = ltbBUS.KTLoaiTBDien(ltbDTO);
                if (kt == 0)
                {
                    ltbBUS.ThemLoaiTBDien(ltbDTO);

                    txtLoaiTB.Text = "";
                }
                else
                {
                    MessageBox.Show("Loại " + txtLoaiTB.Text + " đã có", "Thông báo");
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập loại!", "Thông báo");
                txtLoaiTB.Focus();
            }

            LoadLoaiTBDien_GV();
        }

        private void btnDongLoai_Click(object sender, EventArgs e)
        {
            tsThem.Enabled = true;
            tsLoaiTBDien.Enabled = true;
            tsDongTTTB.Enabled = true;

            TrangThai_LoaiTB(true);
            txtLoaiTB.Text = "";

            grbSanPham.Visible = true;
            grbThietBiDien.Visible = false;
            pnLoai.Visible = false;
        }

        LoaiTBDTO ltbDTO1 = new LoaiTBDTO();
        private void gvLoaiTB_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnThemLoai.Visible = false;
            btnSuaLoai.Visible = true;

            int rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                TrangThai(true);

                DataGridViewRow row = gvLoaiTB.Rows[rowIndex];
                ltbDTO1.MALOAI = int.Parse(row.Cells["colMaLoai"].Value.ToString());
                txtLoaiTB.Text = row.Cells["colTenLoai"].Value.ToString();
            }
        }

        private void gvLoaiTB_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                LoaiTBBUS ltbBUS = new LoaiTBBUS();
                LoaiTBDTO ltbDTO = new LoaiTBDTO();
                ltbDTO.MALOAI = Convert.ToInt32(gvLoaiTB.CurrentRow.Cells["colMaLoaiKD"].Value);
                ltbDTO.TENLOAI = gvLoaiTB.CurrentRow.Cells["colTenLoaiKD"].Value.ToString();

                int kt = ltbBUS.KTMaLoaiTBDien(ltbDTO);
                if (kt == 1)//loại KD này đang có sp
                {
                    MessageBox.Show("Loại kích điện " + ltbDTO.TENLOAI + " đang có sản phẩm, nên không thể xóa!", "Thông báo");
                }
                else
                    if (MessageBox.Show("Bạn có chắc muốn xóa!", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ltbBUS.XoaLoaiTBDien(ltbDTO);
                        if (ltbDTO.TENLOAI == txtLoaiTB.Text)
                            txtLoaiTB.Text = "";
                    }
                LoadLoaiTBDien_GV();
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            reset();

            //btnThem.Visible= true;
            //btnSua.Visible = false;
        }
    }
}
